package com.wskj.web.controller.workbench;


import com.wskj.common.annotation.Log;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.workbench.bo.web.TbUserTiktokAddBo;
import com.wskj.workbench.bo.web.TbUserTiktokEditBo;
import com.wskj.workbench.bo.web.TbUserTiktokQueryBo;
import com.wskj.workbench.service.ITbUserTiktokService;
import com.wskj.workbench.vo.web.TbUserTiktokVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户对应的抖音号Controller
 *
 * @author wskj
 * @date 2024-09-24
 */
@Api(value = "用户对应的抖音号控制器", tags = {"用户对应的抖音号管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/workbench/userTiktok")
public class TbUserTiktokController extends BaseController {

    private final ITbUserTiktokService iTbUserTiktokService;

    /**
     * 查询用户对应的抖音号列表
     */
    @ApiOperation("查询用户对应的抖音号列表")
    @PreAuthorize("@ss.hasPermi('workbench:userTiktok:list')")
    @GetMapping("/list")
    public TableDataInfo<TbUserTiktokVo> list(@Validated TbUserTiktokQueryBo bo) {
        return iTbUserTiktokService.queryPageList(bo);
    }

    /**
     * 导出用户对应的抖音号列表
     */
    @ApiOperation("导出用户对应的抖音号列表")
    @PreAuthorize("@ss.hasPermi('workbench:userTiktok:export')")
    @Log(title = "用户对应的抖音号", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated TbUserTiktokQueryBo bo) {
        List<TbUserTiktokVo> list = iTbUserTiktokService.queryList(bo);
        ExcelUtil<TbUserTiktokVo> util = new ExcelUtil<>(TbUserTiktokVo.class);
        return util.exportExcel(list, "用户对应的抖音号");
    }

    /**
     * 获取用户对应的抖音号详细信息
     */
    @ApiOperation("获取用户对应的抖音号详细信息")
    @PreAuthorize("@ss.hasPermi('workbench:userTiktok:query')")
    @GetMapping("/{id}")
    public AjaxResult<TbUserTiktokVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iTbUserTiktokService.queryById(id));
    }

    /**
     * 新增用户对应的抖音号
     */
    @ApiOperation("新增用户对应的抖音号")
    @PreAuthorize("@ss.hasPermi('workbench:userTiktok:add')")
    @Log(title = "用户对应的抖音号", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody TbUserTiktokAddBo bo) {
        return toAjax(iTbUserTiktokService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改用户对应的抖音号
     */
    @ApiOperation("修改用户对应的抖音号")
    @PreAuthorize("@ss.hasPermi('workbench:userTiktok:edit')")
    @Log(title = "用户对应的抖音号", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody TbUserTiktokEditBo bo) {
        return toAjax(iTbUserTiktokService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除用户对应的抖音号
     */
    @ApiOperation("删除用户对应的抖音号")
    @PreAuthorize("@ss.hasPermi('workbench:userTiktok:remove')")
    @Log(title = "用户对应的抖音号" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iTbUserTiktokService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 导入用户对应的抖音号数据
     */
    @ApiOperation("导入用户对应的抖音号数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('workbench:userTiktok:import')")
    @Log(title = "用户对应的抖音号" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<TbUserTiktokVo> util = new ExcelUtil<>(TbUserTiktokVo.class);
        List<TbUserTiktokVo> tbUserTiktokList;
        try {
            tbUserTiktokList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iTbUserTiktokService.importTbUserTiktok(tbUserTiktokList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<TbUserTiktokVo> util = new ExcelUtil<>(TbUserTiktokVo.class);
        return util.importTemplateExcel("用户对应的抖音号数据");
    }
}
